home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / AVIOutputPreview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  4.6 KB  |  185 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "VirtualDub.h"
  19.  
  20. #include <crtdbg.h>
  21.  
  22. #include "AVIOutput.h"
  23. #include "AVIOutputPreview.h"
  24.  
  25. AVIAudioPreviewOutputStream::AVIAudioPreviewOutputStream(AVIOutput *out) : AVIAudioOutputStream(out) {
  26.     initialized = started = FALSE;
  27.     myAudioOut = NULL;
  28.     fInitialized = false;
  29. }
  30.  
  31. AVIAudioPreviewOutputStream::~AVIAudioPreviewOutputStream() {
  32.     delete myAudioOut;
  33.     myAudioOut = NULL;
  34. }
  35.  
  36. BOOL AVIAudioPreviewOutputStream::init() {
  37.     fInitialized = true;
  38.  
  39.     return TRUE;
  40. }
  41.  
  42. bool AVIAudioPreviewOutputStream::initAudio() {
  43.     const WAVEFORMATEX *pwfex = getWaveFormat();
  44.     int blocks;
  45.     int blocksin512;
  46.  
  47.     // Figure out what a 'good' buffer size is.
  48.     // About a 5th of a second sounds good.
  49.  
  50.     blocks = (pwfex->nAvgBytesPerSec/5 + pwfex->nBlockAlign/2) / pwfex->nBlockAlign;
  51.  
  52.     // How many blocks for 512 bytes?  We don't want buffers smaller than that.
  53.  
  54.     blocksin512 = (512 + pwfex->nBlockAlign - 1) / pwfex->nBlockAlign;
  55.  
  56.     // Use the smaller value and allocate.
  57.  
  58.     myAudioOut = new AVIAudioOutput(max(blocks, blocksin512)*pwfex->nBlockAlign, 10);
  59.  
  60.     return !!myAudioOut;
  61. }
  62.  
  63. BOOL AVIAudioPreviewOutputStream::write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples) {
  64.     fInitialized = true;
  65.     if (!myAudioOut && !initAudio()) {
  66.         return FALSE;
  67.     }
  68.     if (!initialized) {
  69.         if (!myAudioOut->init(getWaveFormat())) {
  70. #if 0
  71.             delete myAudioOut;
  72.             myAudioOut = NULL;
  73.             return FALSE;
  74. #else
  75.             myAudioOut->go_silent();
  76. #endif
  77.         }
  78.         initialized = TRUE;
  79.     }
  80.  
  81.     myAudioOut->write(lpBuffer, cbBuffer, INFINITE);
  82.  
  83.     return TRUE;
  84. }
  85.  
  86. BOOL AVIAudioPreviewOutputStream::isSilent() {
  87.     return myAudioOut == NULL || myAudioOut->isSilent();
  88. }
  89.  
  90. void AVIAudioPreviewOutputStream::start() {
  91.     if (started || !fInitialized) return;
  92.  
  93.     if (!getWaveFormat()) return;
  94.  
  95.     if (!myAudioOut && !initAudio()) {
  96.         return;
  97.     }
  98.  
  99.     if (!initialized) {
  100.         if (!myAudioOut->init(getWaveFormat())) {
  101.             delete myAudioOut;
  102.             myAudioOut = NULL;
  103.             return;
  104.         }
  105.         initialized = TRUE;
  106.     }
  107.  
  108.     if (!myAudioOut->start()) {
  109.         delete myAudioOut;
  110.         myAudioOut = NULL;
  111.     }
  112.  
  113.     started = TRUE;
  114. }
  115.  
  116. void AVIAudioPreviewOutputStream::stop() {
  117.     if (started && myAudioOut)
  118.         myAudioOut->stop();
  119.  
  120.     started = FALSE;
  121.  
  122. }
  123.  
  124. BOOL AVIAudioPreviewOutputStream::flush() {
  125.     _RPT0(0,"AVIAudioPreviewOutputStream: flushing...\n");
  126.     if (myAudioOut && started) myAudioOut->flush();
  127.  
  128.     return TRUE;
  129. }
  130.  
  131. BOOL AVIAudioPreviewOutputStream::finalize() {
  132.     _RPT0(0,"AVIAudioPreviewOutputStream: finalizing...\n");
  133.     if (myAudioOut && started) return myAudioOut->finalize(INFINITE);
  134.  
  135.     return TRUE;
  136. }
  137.  
  138. long AVIAudioPreviewOutputStream::getPosition() {
  139.     return myAudioOut ? myAudioOut->position() : 0;
  140. }
  141.  
  142. bool AVIAudioPreviewOutputStream::isFrozen() {
  143.     return myAudioOut ? myAudioOut->isFrozen() : true;
  144. }
  145.  
  146. /////////////////////////////
  147.  
  148. BOOL AVIVideoPreviewOutputStream::write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples) {
  149.     ((AVIAudioPreviewOutputStream *)output->audioOut)->start();
  150.     return TRUE;
  151. }
  152.  
  153. /////////////////////////////
  154.  
  155. AVIOutputPreview::AVIOutputPreview() {
  156. }
  157.  
  158. AVIOutputPreview::~AVIOutputPreview() {
  159. }
  160.  
  161. BOOL AVIOutputPreview::initOutputStreams() {
  162.     if (!(audioOut = new AVIAudioPreviewOutputStream(this))) return FALSE;
  163.     if (!(videoOut = new AVIVideoPreviewOutputStream(this))) return FALSE;
  164.  
  165.     return TRUE;
  166. }
  167.  
  168. BOOL AVIOutputPreview::init(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved) {
  169.     return TRUE;
  170. }
  171.  
  172. BOOL AVIOutputPreview::finalize() {
  173.     _RPT0(0,"AVIOutputPreview: Finalizing...\n");
  174.  
  175.     if (audioOut && !audioOut->finalize())
  176.         return FALSE;
  177.  
  178.     return TRUE;
  179. }
  180.  
  181. BOOL AVIOutputPreview::isPreview() { return TRUE; }
  182.  
  183. void AVIOutputPreview::writeIndexedChunk(FOURCC ckid, LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer) {
  184. }
  185.